//
//  YBLoginVerifyZjcViewController.m
//  Nectar
//
//  Created by famer on 2023/11/7.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBLoginVerifyZjcViewController+LayoutConstraints.h"
#import "YBNextStepView.h"
#import "YBNavigationBar.h"

@interface YBLoginVerifyZjcViewController ()
@property(copy, nonatomic) NSString *zjc1;
@property(copy, nonatomic) NSString *zjc2;
@property(copy, nonatomic) NSString *zjc3;

@property(copy, nonatomic) NSString *zjcTemp1;
@property(copy, nonatomic) NSString *zjcTemp2;
@property(copy, nonatomic) NSString *zjcTemp3;
@end

@implementation YBLoginVerifyZjcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setCleanEdgesForExtendedLayout];
    
    [self analysicZjc];
    
    //    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.createWalletLabel];
    [self.view addSubview:self.backupWalletImageView];
    [self.view addSubview:self.backupWalletLabel];
    
    [self.view addSubview:self.zjcContentView];
    [self.zjcContentView addSubview:self.indexLabel1];
    [self.zjcContentView addSubview:self.row1Button1];
    [self.zjcContentView addSubview:self.row1Button2];
    [self.zjcContentView addSubview:self.row1Button3];
    
    [self.zjcContentView addSubview:self.indexLabel2];
    [self.zjcContentView addSubview:self.row2Button1];
    [self.zjcContentView addSubview:self.row2Button2];
    [self.zjcContentView addSubview:self.row2Button3];
    
    [self.zjcContentView addSubview:self.indexLabel3];
    [self.zjcContentView addSubview:self.row3Button1];
    [self.zjcContentView addSubview:self.row3Button2];
    [self.zjcContentView addSubview:self.row3Button3];
    
    [self.view addSubview:self.nextStepView];
    
    [self.nextStepView setNextStepButtonEnabled];
    [self setLayoutConstraints];
}

#pragma mark - Private Method
- (void)doLogin{
    [self.view endEditing:YES];
    
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    [params setObject:self.email forKey:@"email"];
    [params setObject:self.password forKey:@"password"];
    [params setObject:self.zjc forKey:@"zjc"];
        
    NSString *urlString = [NSString stringWithFormat:@"%@",YB_DO_LOGIN_URL];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        YBLog(@"SUCCESS: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSLog(@"-----%@", obj.data);
            NSLog(@"登录成功，去首页");
            //去首页
        }
        
    } failure:^(NSError *error) {
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)doVerify{
    //已选择的zjc
    NSString *temp1 = [NSObject nullSafeForObject:self.zjcTemp1];
    NSString *temp2 = [NSObject nullSafeForObject:self.zjcTemp2];
    NSString *temp3 = [NSObject nullSafeForObject:self.zjcTemp3];
    
    if(temp1.length==0 || temp2.length == 0 || temp3.length == 0){
        return;
    }

    //判断是否选对
    if([self.zjc1 isEqualToString:temp1] &&
       [self.zjc2 isEqualToString:temp2] &&
       [self.zjc3 isEqualToString:temp3]){
        [self doLogin];
    }
}

- (void)onRowButtonClick:(UIButton *)actionButton{
    [self setButtonSelected:actionButton];
    if([actionButton isEqual:self.row1Button1]){
        [self setButtonUnSelect:self.row1Button2];
        [self setButtonUnSelect:self.row1Button3];

    } else if([actionButton isEqual:self.row1Button2]){
        [self setButtonUnSelect:self.row1Button1];
        [self setButtonUnSelect:self.row1Button3];
        
    } else if([actionButton isEqual:self.row1Button3]){
        [self setButtonUnSelect:self.row1Button2];
        [self setButtonUnSelect:self.row1Button1];
        
    } else if([actionButton isEqual:self.row2Button1]){
        [self setButtonUnSelect:self.row2Button2];
        [self setButtonUnSelect:self.row2Button3];
        
    } else if([actionButton isEqual:self.row2Button2]){
        [self setButtonUnSelect:self.row2Button1];
        [self setButtonUnSelect:self.row2Button3];
        
    } else if([actionButton isEqual:self.row2Button3]){
        [self setButtonUnSelect:self.row2Button1];
        [self setButtonUnSelect:self.row2Button2];
        
    } else if([actionButton isEqual:self.row3Button1]){
        [self setButtonUnSelect:self.row3Button2];
        [self setButtonUnSelect:self.row3Button3];
        
    } else if([actionButton isEqual:self.row3Button2]){
        [self setButtonUnSelect:self.row3Button1];
        [self setButtonUnSelect:self.row3Button3];
        
    } else if([actionButton isEqual:self.row3Button3]){
        [self setButtonUnSelect:self.row3Button1];
        [self setButtonUnSelect:self.row3Button2];
    }
}

- (void)analysicZjc{
    
    NSArray *allList = [self.zjc componentsSeparatedByString:@","];

    // 生成3个随机数
    NSUInteger num1 = arc4random_uniform((uint32_t)allList.count);
    NSUInteger num2 = arc4random_uniform((uint32_t)allList.count);
    while (num1 == num2) {
        num2 = arc4random_uniform((uint32_t)allList.count);
    }

    NSUInteger num3 = arc4random_uniform((uint32_t)allList.count);
    while (num1 == num3 || num2 == num3) {
        num3 = arc4random_uniform((uint32_t)allList.count);
    }
    NSLog(@"=====3个随机数：%@ %@ %@", @(num1), @(num2), @(num3));

    // 筛选剩下的项
    NSMutableArray *allListTemp = [NSMutableArray array];
    for (NSUInteger index = 0; index < allList.count; index++) {
        BOOL isNum1 = (num1 == index);
        BOOL isNum2 = (num2 == index);
        BOOL isNum3 = (num3 == index);

        if (!isNum1 && !isNum2 && !isNum3) {
            [allListTemp addObject:allList[index]];
        }
    }

    // 从剩下的项中选出9个值
    NSUInteger item1 = arc4random_uniform((uint32_t)allListTemp.count);
    NSUInteger item2 = arc4random_uniform((uint32_t)allListTemp.count);
    while (item1 == item2) {
        item2 = arc4random_uniform((uint32_t)allListTemp.count);
    }

    NSUInteger item3 = arc4random_uniform((uint32_t)allListTemp.count);
    while (item1 == item3 || item2 == item3) {
        item3 = arc4random_uniform((uint32_t)allListTemp.count);
    }

    NSUInteger item4 = arc4random_uniform((uint32_t)allListTemp.count);
    while (item1 == item4 || item2 == item4 || item3 == item4) {
        item4 = arc4random_uniform((uint32_t)allListTemp.count);
    }

    NSUInteger item5 = arc4random_uniform((uint32_t)allListTemp.count);
    while (item1 == item5 || item2 == item5 || item3 == item5 || item4 == item5) {
        item5 = arc4random_uniform((uint32_t)allListTemp.count);
    }

    NSUInteger item6 = arc4random_uniform((uint32_t)allListTemp.count);
    while (item1 == item6 || item2 == item6 || item3 == item6 || item4 == item6 || item5 == item6) {
        item6 = arc4random_uniform((uint32_t)allListTemp.count);
    }

    //组装3行
    NSUInteger row1Real = arc4random_uniform((uint32_t)3);
    NSMutableArray *row1 = [NSMutableArray array];
    [row1 addObject:@(item1)];
    [row1 addObject:@(item2)];
    [row1 insertObject:@(num1) atIndex:row1Real];

    NSUInteger row2Real = arc4random_uniform((uint32_t)3);
    NSMutableArray *row2 = [NSMutableArray array];
    [row2 addObject:@(item3)];
    [row2 addObject:@(item4)];
    [row2 insertObject:@(num2) atIndex:row2Real];
    
    NSUInteger row3Real = arc4random_uniform((uint32_t)3);
    NSMutableArray *row3 = [NSMutableArray array];
    [row3 addObject:@(item5)];
    [row3 addObject:@(item6)];
    [row3 insertObject:@(num3) atIndex:row3Real];
    
    //初始化
    self.indexLabel1.text = [NSString stringWithFormat:@"%@", [NSString to00Format:(num1+1)]];
    [self.row1Button1 setTitle:[allList objectAtIndex:[row1[0] integerValue]] forState:UIControlStateNormal];
    [self.row1Button1 setTitle:[allList objectAtIndex:[row1[0] integerValue]] forState:UIControlStateSelected];
    [self.row1Button2 setTitle:[allList objectAtIndex:[row1[1] integerValue]] forState:UIControlStateNormal];
    [self.row1Button2 setTitle:[allList objectAtIndex:[row1[1] integerValue]] forState:UIControlStateSelected];
    [self.row1Button3 setTitle:[allList objectAtIndex:[row1[2] integerValue]] forState:UIControlStateNormal];
    [self.row1Button3 setTitle:[allList objectAtIndex:[row1[2] integerValue]] forState:UIControlStateSelected];
    
    self.indexLabel2.text = [NSString stringWithFormat:@"%@", [NSString to00Format:(num2+1)]];
    [self.row2Button1 setTitle:[allList objectAtIndex:[row2[0] integerValue]] forState:UIControlStateNormal];
    [self.row2Button1 setTitle:[allList objectAtIndex:[row2[0] integerValue]] forState:UIControlStateSelected];
    [self.row2Button2 setTitle:[allList objectAtIndex:[row2[1] integerValue]] forState:UIControlStateNormal];
    [self.row2Button2 setTitle:[allList objectAtIndex:[row2[1] integerValue]] forState:UIControlStateSelected];
    [self.row2Button3 setTitle:[allList objectAtIndex:[row2[2] integerValue]] forState:UIControlStateNormal];
    [self.row2Button3 setTitle:[allList objectAtIndex:[row2[2] integerValue]] forState:UIControlStateSelected];
    
    self.indexLabel3.text = [NSString stringWithFormat:@"%@", [NSString to00Format:(num3+1)]];
    [self.row3Button1 setTitle:[allList objectAtIndex:[row3[0] integerValue]] forState:UIControlStateNormal];
    [self.row3Button1 setTitle:[allList objectAtIndex:[row3[0] integerValue]] forState:UIControlStateSelected];
    [self.row3Button2 setTitle:[allList objectAtIndex:[row3[1] integerValue]] forState:UIControlStateNormal];
    [self.row3Button2 setTitle:[allList objectAtIndex:[row3[1] integerValue]] forState:UIControlStateSelected];
    [self.row3Button3 setTitle:[allList objectAtIndex:[row3[2] integerValue]] forState:UIControlStateNormal];
    [self.row3Button3 setTitle:[allList objectAtIndex:[row3[2] integerValue]] forState:UIControlStateSelected];
}

- (void)setButtonSelected:(UIButton *)button{
    [button setSelected:YES];
    [button setBackgroundColor:kThemeColor];
    [button.layer setBorderColor:kThemeColor.CGColor];
}

- (void)setButtonUnSelect:(UIButton *)button{
    [button setSelected:NO];
    [button setBackgroundColor:[UIColor hexColor:@"FBFCFE"]];
    [button.layer setBorderColor:[UIColor hexColor:@"F6F5F6"].CGColor];
}


#pragma mark - Getter Setter
YBLabelGetter(createWalletLabel, _createWalletLabel, [UIFont boldSystemFontOfSize:20], @"Create Wallet", [UIColor hexColor:@"1E1E1E"], NSTextAlignmentLeft);

YBImageViewGetter(backupWalletImageView, _backupWalletImageView, [UIImage imageNamed:@"icon_create_wallet_verify"]);
YBLabelGetter(backupWalletLabel, _backupWalletLabel, [UIFont systemFontOfSize:16], @"Verify your mnemonic phrase", [UIColor hexColor:@"1E1E1E"], NSTextAlignmentLeft);

YBLabelGetter(indexLabel1, _indexLabel1, [UIFont boldSystemFontOfSize:18], @"1", [UIColor hexColor:@"1E1E1E"], NSTextAlignmentLeft);
YBLabelGetter(indexLabel2, _indexLabel2, [UIFont boldSystemFontOfSize:18], @"2", [UIColor hexColor:@"1E1E1E"], NSTextAlignmentLeft);
YBLabelGetter(indexLabel3, _indexLabel3, [UIFont boldSystemFontOfSize:18], @"3", [UIColor hexColor:@"1E1E1E"], NSTextAlignmentLeft);

YBViewGetter(zjcContentView, _zjcContentView, [UIColor hexColor:@"FFFFFF"])

- (UIButton *)row1Button1{
    if(!_row1Button1){
        _row1Button1 = [self generateButton];
    }
    return _row1Button1;
}

- (UIButton *)row1Button2{
    if(!_row1Button2){
        _row1Button2 = [self generateButton];
    }
    return _row1Button2;
}

- (UIButton *)row1Button3{
    if(!_row1Button3){
        _row1Button3 = [self generateButton];
    }
    return _row1Button3;
}

- (UIButton *)row2Button1{
    if(!_row2Button1){
        _row2Button1 = [self generateButton];
    }
    return _row2Button1;
}

- (UIButton *)row2Button2{
    if(!_row2Button2){
        _row2Button2 = [self generateButton];
    }
    return _row2Button2;
}

- (UIButton *)row2Button3{
    if(!_row2Button3){
        _row2Button3 = [self generateButton];
    }
    return _row2Button3;
}

- (UIButton *)row3Button1{
    if(!_row3Button1){
        _row3Button1 = [self generateButton];
    }
    return _row3Button1;
}

- (UIButton *)row3Button2{
    if(!_row3Button2){
        _row3Button2 = [self generateButton];
    }
    return _row3Button2;
}

- (UIButton *)row3Button3{
    if(!_row3Button3){
        _row3Button3 = [self generateButton];
    }
    return _row3Button3;
}

- (UIButton *)generateButton{
    
    UIButton *rowButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rowButton.backgroundColor = [UIColor clearColor];
    [rowButton setTitle:@"" forState:UIControlStateNormal];
    [rowButton setBackgroundColor:[UIColor hexColor:@"FBFCFE"]];
    rowButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rowButton.layer setBorderColor:[UIColor hexColor:@"F6F5F6"].CGColor];
    [rowButton.layer setBorderWidth:1.0];
    [rowButton.layer setCornerRadius:6];
    [rowButton setTitleColor:[UIColor hexColor:@"1E1E1E"] forState:UIControlStateNormal];
    [rowButton setTitleColor:[UIColor hexColor:@"FFFFFF"] forState:UIControlStateSelected];
    [rowButton addTarget:self action:@selector(onRowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    return rowButton;
}

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        [_navigationBar setTitle:kLogin titleColor:[UIColor blackColor]];
        [_navigationBar setRightBarTitle:@"" forState:UIControlStateNormal];
        [_navigationBar setRightBarTitleColor:[UIColor hexColor:@"0099ff"] forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController dismissViewControllerAnimated:YES completion:nil];
        };
        _navigationBar.onRightBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}

- (YBNextStepView *)nextStepView{
    if(!_nextStepView){
        _nextStepView = [[YBNextStepView alloc] initWithFrame:CGRectZero];
        [_nextStepView setButtonTitle:kBtnNext];
        [_nextStepView setNextStepButtonUnabled];
        __weak __typeof(&*self)weakSelf = self;
        _nextStepView.onNextStepBlock = ^{
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            //去验证
            [strongSelf doVerify];
        };
    }
    return _nextStepView;
}
@end
